<script>
export default {
  name: "AddLobbyTypeView",
  data() {
    // 排序号只能是整数数字
    var validateSort = (rule, value, callback) => {
      let numSort = Number(value);
      if (value.length === 0){
        callback(new Error('请输入排序号'));
      }
      if (isNaN(numSort) || !(numSort === parseInt(value))){
        callback(new Error('只能是整数数字'));
      }
      if (numSort === parseInt(value)){
        callback();
      }
    };
    return {
      loading: false,
      form: {
        lobbyTypeName: '',
        lobbyTypeDescribe: '',
        sort:''
      },
      rules: {
        lobbyTypeName: [
          {required: true, message: '影厅类型不能为空', trigger: 'blur'},
        ],
        lobbyTypeDescribe: [
          {required: true, message: '影厅类型描述不能为空', trigger: 'blur'},
        ],
        sort: [{required: true, validator: validateSort, trigger: 'blur' }],
      },
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          axios.post('/manage/addLobbyType', {
            lobbyTypeName: this.form.lobbyTypeName,
            lobbyTypeDescribe: this.form.lobbyTypeDescribe,
            sort: this.form.sort,
          })
              .then(response => {
                this.loading = false;
                let data = response.data;
                // 添加失败
                if (data.code!== 200) {
                  this.$message.error(data.msg);
                  return;
                }
                // 添加成功
                this.$message({
                  message: data.msg,
                  type: 'success'
                });
                // 跳转页面
                this.$router.push({
                  path: '/lobbyType/list'
                });
              })
              .catch(error => {
                this.loading = false;
                console.log(error)
                this.$message.error('系统异常，请联系管理员或稍后再试');
              })
              .finally(() => this.loading = false);
        } else {
          this.loading = false;
          return false;
        }
      });
    }
  }
}
</script>

<template>
  <el-form ref="form" v-loading="loading" :rules="rules" :model="form" label-width="80px">
    <el-form-item label="影厅类型" prop="lobbyTypeName">
      <el-input v-model="form.lobbyTypeName" placeholder="请输入影厅类型"></el-input>
    </el-form-item>
    <el-form-item label="影厅描述" prop="lobbyTypeDescribe">
      <el-input v-model="form.lobbyTypeDescribe" placeholder="请输入影厅描述"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="form.sort" placeholder="请输入整数"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('form')">添加</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>

</style>